package com.mxt.moviedemo.controller;

import com.mxt.moviedemo.entity.Movies;
import com.mxt.moviedemo.entity.Reviews;
import com.mxt.moviedemo.entity.User;
import com.mxt.moviedemo.service.MoviesService;
import com.mxt.moviedemo.service.ReviewsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("movie")
public class MoviesController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private MoviesService moviesService;
    @GetMapping("/infoMovie")
    public Map<String, Object> getUserInfo(@RequestParam("mid") int mid) {
        logger.info("获取电影信息，id：{}", mid);
        Map<String, Object> result = new HashMap<>();

        try {
            Movies movies=moviesService.getMovieById(mid);
            if (movies != null) {
                result.put("success", true);
                result.put("movies", movies);
            } else {
                result.put("success", false);
                result.put("error", "未找到该电影");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", "查询电影信息失败：" + e.getMessage());
        }
        return result;
    }
    @GetMapping("/searchMovie")
    public Map<String, Object> searchMovie(@RequestParam("mname") String mname) {
        logger.info("获取电影信息，id：{}", mname);
        Map<String, Object> result = new HashMap<>();

        try {
            List<Movies> movies=moviesService.searchMoviesByName(mname);
            if (movies != null) {
                result.put("success", true);
                result.put("movies", movies);
            } else {
                result.put("success", false);
                result.put("error", "未找到该电影");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", "查询电影信息失败：" + e.getMessage());
        }
        return result;
    }

    @GetMapping("/getMoviePrice")
    public Map<String, Object> getMoviePrice(@RequestParam("mname") String mname) {
        Map<String, Object> result = new HashMap<>();
        try {
            Movies movie = moviesService.getMovieByName(mname);
            if (movie != null) {
                result.put("success", true);
                result.put("price", movie.getPrice());
            } else {
                result.put("success", false);
                result.put("error", "未找到该电影信息");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", "查询电影价格失败：" + e.getMessage());
        }
        return result;
    }

}